package com.fys.api.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * @Description:统一跨域处理
 * CORS: Cross Origin Resource sharing
 * @Date:   2020/8/18 11:35
 * @Author: fys
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();

        //设置是否允许cookie跨域
        config.setAllowCredentials(true);
        //设置允许跨域的头部
        config.setAllowedHeaders(Arrays.asList("*"));
        // 设置允许的方法：get,post
        config.setAllowedMethods(Arrays.asList("*"));
        // 设置允许的域：如http://www.baidu.com
        config.setAllowedOrigins(Arrays.asList("*"));
        // 设置缓存时间，在这个时间段内相同的跨域请求就不会检查了
        config.setMaxAge(300l);

        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }




}
